package com.aiden.cashbook.data.charge

import com.aiden.cashbook.bean.Bill
import com.aiden.cashbook.bean.UseType
import com.aiden.cashbook.dao.BillDao
import com.aiden.cashbook.dao.UseTypeDao
import com.aiden.cashbook.db.DaoHelper

class ChargeLocalDataSource: ChargeDataSource {
    override fun chargeAccount(bill: Bill, callback: ChargeDataSource.SavedCallback) {
        try {
            DaoHelper.getBillDao().save(bill)
            callback.onSavedSuccess()
        } catch (e: Exception) {
            e.printStackTrace()
            callback.onFailure()
        }
    }

    override fun accountType(type: String, callback: ChargeDataSource.LoadListCallBack<UseType>) {

        val list: List<UseType> = DaoHelper.getTypeDao().queryBuilder().where(UseTypeDao.Properties.Type.eq(type)).build().list()
        if (list.isNotEmpty()) {
            callback.onDataLoaded(list)
        } else {
            callback.onDataNotAvailable()
        }
    }

    override fun billList(userId: Long, page: Int, size: Int, callback: ChargeDataSource.LoadListCallBack<Bill>) {

        try {
            val billList = DaoHelper.getBillDao().queryBuilder().where(BillDao.Properties.UserId.eq(userId)).offset(page * size).limit(10).list()
            callback.onDataLoaded(billList)
        } catch (e: Exception) {
            e.printStackTrace()
            callback.onDataNotAvailable()
        }
    }
}